﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AIC_CD
{
    public partial class UCDanhMuc : System.Web.UI.UserControl
    {
        private static int ID;
        private string MyAction = MyConst.Action.Addnew;
        private string MyMaDM = "";

        #region Ham Su kien
        protected void Page_Load(object sender, EventArgs e)
        {
            formUpdate.Visible = false;
            if (Request.QueryString["DanhMuc"] != null)
            {
                MyMaDM = Request.QueryString["DanhMuc"];
                CheckPermision();
                Config();
                if (!IsPostBack)
                {
                    BindDataToGrid(MyMaDM, "");
                }
            }
        }

        private void Config()
        {
            if (MyMaDM == MyConst.DanhMuc.CapBac.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.CapBac.Ten;
            else if (MyMaDM == MyConst.DanhMuc.Quyen.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.Quyen.Ten;
            else if (MyMaDM == MyConst.DanhMuc.NhomUser.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.NhomUser.Ten;
            else if (MyMaDM == MyConst.DanhMuc.TrinhDo.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.TrinhDo.Ten;
            else if (MyMaDM == MyConst.DanhMuc.ChucVu.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.ChucVu.Ten;
            else if (MyMaDM == MyConst.DanhMuc.BaoCaoDinhKem.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.BaoCaoDinhKem.Ten;
            else if (MyMaDM == MyConst.DanhMuc.NoiDungMau1.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.NoiDungMau1.Ten;
            else if (MyMaDM == MyConst.DanhMuc.NoiDungMau8.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.NoiDungMau8.Ten;
            else if (MyMaDM == MyConst.DanhMuc.KyBaoCao.Ma)
                lblFormName.InnerText = "Quản trị danh mục " + MyConst.DanhMuc.KyBaoCao.Ten;
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Index.aspx");
        }


        protected void grdDanhMuc_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            ID = Convert.ToInt32(e.CommandArgument);


            if (e.CommandName == MyConst.Action.Update)
            {
                formUpdate.Visible = true;
                DataTable DanhMucTable = GetChiTietDanhMuc(ID);
                txtMaDanhMuc.Text = DanhMucTable.Rows[0][1].ToString();
                txtTenDanhMuc.Text = DanhMucTable.Rows[0][2].ToString();
                btnLuu.CommandName = MyConst.Action.Update;
            }
            else if (e.CommandName == MyConst.Action.Delete)
            {
                string sql = "DELETE FROM dbo.tblDanhMuc WHERE ID = @ID";
                List<SqlParameter> lstParameter = new List<SqlParameter>();
                lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, ID));
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);
                BindDataToGrid(MyMaDM, "");
            }
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            formUpdate.Visible = true;
            txtMaDanhMuc.Text = MyMaDM;
            txtTenDanhMuc.Text = "";
            btnLuu.CommandName = MyConst.Action.Addnew;
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = Convert.ToInt32(Session["userid"]);
                if (btnLuu.CommandName == MyConst.Action.Addnew)
                {
                    InsertDanhMuc(MyMaDM, txtTenDanhMuc.Text, UserID);
                    BindDataToGrid(MyMaDM, "");
                    MyCommon.AddMessage(this.Page.Master, "Thêm mới thành công !", MyConst.MessageType.Success);
                }
                else if (btnLuu.CommandName == MyConst.Action.Update)
                {
                    UpdateDanhMuc(ID, txtTenDanhMuc.Text, UserID);
                    BindDataToGrid(MyMaDM, "");
                    MyCommon.AddMessage(this.Page.Master, "Cập nhật thành công !", MyConst.MessageType.Success);
                }
            }
            catch (Exception)
            {
                MyCommon.AddMessage(this.Page.Master, "Lỗi hệ thống !", MyConst.MessageType.Error);
            }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            formUpdate.Visible = false;
        }
        #endregion Ham su kien

        #region Ham chuc nang

        protected void BindDataToGrid(string MaDanhMuc, string TenDanhMuc)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@MaDM", SqlDbType.NVarChar, ParameterDirection.Input, MaDanhMuc));
            arrParams.Add(SqlHelper.CreateParameterObject("@TenDM", SqlDbType.NVarChar, ParameterDirection.Input, TenDanhMuc));
            string sql = "SELECT DM.ID ,DM.MaDM ,DM.TenDM , " +
                         "CONVERT(NVARCHAR(10),DM.CreateDate,103)+' '+ CONVERT(NVARCHAR(10),DM.CreateDate,108) CreateDate, " +
                         "CONVERT(NVARCHAR(10),DM.UpdateDate ,103)+' '+ CONVERT(NVARCHAR(10),DM.UpdateDate ,108) UpdateDate, " +
                         "U.HoTen " +
                         "FROM dbo.tblDanhMuc DM " +
                         "LEFT JOIN dbo.tblUser U ON U.id = DM.UserID " +
                         "WHERE DM.MaDM = @MaDM AND (DM.TenDM like N'%'+@TenDM+'%' OR @TenDM = '')";
            grdDanhMuc.DataSource = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            grdDanhMuc.DataBind();
        }

        protected DataTable GetChiTietDanhMuc(int ID)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, ID));
            return SqlHelper.ExecuteDataTable(CommandType.Text, "Select * from tblDanhMuc where ID = @ID", arrParams);
        }

        protected void InsertDanhMuc(string MaDanhMuc, string TenDanhMuc, int UserID)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@MaDanhMuc", SqlDbType.NVarChar, ParameterDirection.Input, MaDanhMuc));
            arrParams.Add(SqlHelper.CreateParameterObject("@TenDanhMuc", SqlDbType.NVarChar, ParameterDirection.Input, TenDanhMuc));
            arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, UserID));
            string sql = "INSERT  INTO dbo.tblDanhMuc(" +
                         "MaDM,TenDM,CreateDate,UpdateDate,UserID)" +
                         "VALUES  (@MaDanhMuc,@TenDanhMuc,GETDATE(),GETDATE(),@UserID)";
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
        }

        protected void UpdateDanhMuc(int IDDanhMuc, string TenDanhMuc, int UserID)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@IDDanhMuc", SqlDbType.Int, ParameterDirection.Input, IDDanhMuc));
            arrParams.Add(SqlHelper.CreateParameterObject("@TenDanhMuc", SqlDbType.NVarChar, ParameterDirection.Input, TenDanhMuc));
            arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, UserID));
            string sql = "UPDATE dbo.tblDanhMuc SET TenDM=@TenDanhMuc, UpdateDate=GETDATE(),UserID=@UserID WHERE ID = @IDDanhMuc";
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
        }

        public void CheckPermision()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("CanBoDuocBoiDuongDaoTao");
            if (Role == null || Role["Xem"] == 0 || ((Role["Xem"] == 1 && Role["Sua"] == 0)))
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }
        #endregion  Ham chuc nang

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDataToGrid(MyMaDM, txtSearch.Text.Trim());
        }
    }
}